package com.xnyzc.lhy.order.service.impl.evaluation;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.DiDiException;
import com.xnyzc.lhy.common.penum.EDiDiErrorCode;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.order.entity.mq.OaCOrderPersonal;
import com.xnyzc.lhy.order.entity.order.OaOrderEvaluation;
import com.xnyzc.lhy.order.entity.param.comment.EvaluationParam;
import com.xnyzc.lhy.order.mapper.order.OaOrderEvaluationMapper;
import com.xnyzc.lhy.order.service.evaluation.EvaluationService;
import com.xnyzc.lhy.order.service.order.IOaDOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

/**
 * <p>
 * 评价相关
 * </p>
 *
 * @author wangshuo
 * @since 2019-09-25
 */
@Service
public class EvaluationServiceImpl implements EvaluationService {

    @Autowired
    private OaOrderEvaluationMapper oaOrderEvaluationMapper;

    @Autowired
    private IOaDOrderService oaDOrderService;

    /**
     * 乘客评价司机
     * @param param 入参实体
     * @return
     */
    @Override
    public Rv addEvaluate(EvaluationParam param){
        if(StringUtils.isBlank(param.getOrderId()) || CheckUtil.objIsEmpty(param.getStar())) {
            return Rv.error(EErrorCode.missingArg);
        }
        OaCOrderPersonal order=oaDOrderService.billDetails(param.getOrderId());
        OaOrderEvaluation evaluation=new OaOrderEvaluation();
        evaluation.setEvaluationId(IDUtil.nextId());
        Date date = new Date();
        evaluation.setCreateTime(date);
        evaluation.setUserId(Long.valueOf(order.getUserId()));
        evaluation.setUpdateTime(date);
        evaluation.setEvaluationScore(String.valueOf(param.getStar()*20));
        evaluation.setOrderNo(order.getOrderNo());
        if (CheckUtil.strIsNotEmpty(param.getComment())) {
            evaluation.setEvaluationContent(param.getComment());
        }
        if (CheckUtil.strIsNotEmpty(param.getComment_tags())) {
            evaluation.setTagCommentContent(param.getComment_tags());
        }
        evaluation.setUpdateUser(Long.valueOf(order.getUserId()));
        evaluation.setCreateUser(Long.valueOf(order.getUserId()));
        evaluation.setUserType(0);
        evaluation.setTag(param.getTag());
        try {
            oaOrderEvaluationMapper.insert(evaluation);
        } catch (Exception e) {
            throw DiDiException.create("用户评价司机失败");
        }
        order.setIsComment(1);
        order.setUpdateTime(date);
        if (!oaDOrderService.updateUserOrderInfo(order)) {
            throw DiDiException.create("修改订单信息失败");
        }
        return Rv.wrap(EErrorCode.success);
    }

}
